package org.deng.learn.concurrent.basic;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @ Author: DengLibin
 * @ Description:
 * Callable接口支持返回执行结果，此时需要调用FutureTask.get()方法实现，
 * 此方法会阻塞主线程直到获取‘将来’结果；当不调用此方法时，主线程不会阻塞！
 * @ DateTime:  13:41 2018/5/8 0008
 */
public class CallableDemo {
    public static  void main(String[] arags) throws ExecutionException, InterruptedException {
        Callable<String> callable= () -> "aa";
        FutureTask<String> task = new FutureTask<>(callable);
        long beginTime = System.currentTimeMillis();
        // 创建线程
        new Thread(task).start();
        // 调用get()阻塞主线程，反之，线程不会阻塞
        String result = task.get();
        long endTime = System.currentTimeMillis();
        System.out.println("hello : " + result);
        System.out.println("cast : " + (endTime - beginTime) + "ms!");
    }
}
